package com.gpframework.module.purchase.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gpframework.common.enums.YesNoEnum;
import com.gpframework.common.utils.DateUtils;
import com.gpframework.common.utils.StringUtils;
import com.gpframework.module.manufacture.domain.GpDyeingFactory;
import com.gpframework.module.manufacture.domain.GpProduceProduct;
import com.gpframework.module.manufacture.domain.GpProductionDetails;
import com.gpframework.module.manufacture.domain.req.GpProductionDetailsUpdate;
import com.gpframework.module.manufacture.service.IGpProduceProductService;
import com.gpframework.module.purchase.domain.GpIngredientsTable;
import com.gpframework.module.purchase.domain.req.GpPurchasingMakingDetailsUpdate;
import com.gpframework.module.purchase.service.IGpIngredientsTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gpframework.module.purchase.mapper.GpPurchasingMakingDetailsMapper;
import com.gpframework.module.purchase.domain.GpPurchasingMakingDetails;
import com.gpframework.module.purchase.domain.vo.GpPurchasingMakingDetailsVo;
import com.gpframework.module.purchase.service.IGpPurchasingMakingDetailsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.ArrayList;
import java.util.List;
/**
 * @ClassName GpPurchasingMakingDetails
 * @Author gpframework
 * @Date 2023-12-22
 * @Version 1.0.0
 * @Description 采购做货明细Service业务层处理
 */
@Service
public class GpPurchasingMakingDetailsServiceImpl extends ServiceImpl<GpPurchasingMakingDetailsMapper, GpPurchasingMakingDetails> implements IGpPurchasingMakingDetailsService {

    @Autowired
    private IGpProduceProductService iGpProduceProductService;
    @Autowired
    private IGpIngredientsTableService iGpIngredientsTableService;

    /**
     * @Description 查询列表返回VO 用于返回给前端的列表接口
     * @Author gpframework
     * @Date 2023-12-22
     * @Param [lqw]
     * @Return List<GpPurchasingMakingDetailsVo>
     */
    @Override
    public List<GpPurchasingMakingDetailsVo> listVo(QueryWrapper<GpPurchasingMakingDetails> lqw) {
        return this.baseMapper.listVo(lqw);
    }

    /**
     * @Description 通过查询详情VO 用于返回给前端的列详情接口
     * @Author gpframework
     * @Date 2023-12-22
     * @Param [id]
     * @Return GpPurchasingMakingDetailsVo
     */
    @Override
    public GpPurchasingMakingDetailsVo getVoById(Long id) {
        return this.baseMapper.getVo(id);
    }

    @Override
    public boolean edit(List<GpPurchasingMakingDetailsUpdate> list) {
        List<GpPurchasingMakingDetails> updateList = new ArrayList<>();
        GpPurchasingMakingDetails gpPurchasingMakingDetails;
        for (GpPurchasingMakingDetailsUpdate gpPurchasingMakingDetailsUpdate : list) {
            gpPurchasingMakingDetails = Convert.convert(new TypeReference<GpPurchasingMakingDetails>() {}, gpPurchasingMakingDetailsUpdate);
            this.init(gpPurchasingMakingDetails);
            updateList.add(gpPurchasingMakingDetails);
        }
        return this.updateBatchById(updateList);
    }

    @Override
    public boolean editOne(GpPurchasingMakingDetailsUpdate gpPurchasingMakingDetailsUpdate) {
        GpPurchasingMakingDetails gpPurchasingMakingDetails = Convert.convert(new TypeReference<GpPurchasingMakingDetails>() {}, gpPurchasingMakingDetailsUpdate);
        this.init(gpPurchasingMakingDetails);
        return this.updateById(gpPurchasingMakingDetails);
    }

    @Override
    public boolean copy(Long id, Integer copyNum) {
        GpPurchasingMakingDetails gpPurchasingMakingDetails = this.getById(id);
        if (gpPurchasingMakingDetails == null) {
            return false;
        }
        for (int i = 0; i < copyNum; i++) {
            gpPurchasingMakingDetails.setId(null);
            gpPurchasingMakingDetails.setSn(this.getSn());
            this.save(gpPurchasingMakingDetails);
        }
        return true;
    }

    // 数据初始化
    private void init(GpPurchasingMakingDetails gpPurchasingMakingDetails) {
        GpPurchasingMakingDetails beforePurchasingMakingDetails = this.getById(gpPurchasingMakingDetails.getId());
        if (StringUtils.isBlank(beforePurchasingMakingDetails.getSn())) {
            // 如果订单号为空，则生成订单号
            gpPurchasingMakingDetails.setSn(this.getSn());
        }
        // 判断是否需要新增产品基础表数据
        if (StringUtils.isNotBlank(gpPurchasingMakingDetails.getProductName()) && StringUtils.isNotBlank(gpPurchasingMakingDetails.getProductColor())) {
            GpProduceProduct gpProduceProduct = iGpProduceProductService.getOne(new LambdaQueryWrapper<GpProduceProduct>()
                    .eq(GpProduceProduct::getProductName, gpPurchasingMakingDetails.getProductName())
                    .eq(GpProduceProduct::getColour, gpPurchasingMakingDetails.getProductColor())
                    .eq(GpProduceProduct::getDelStatus, YesNoEnum.NO.getKey())
                    .last("limit 1"));
            if (gpProduceProduct == null) {
                gpProduceProduct = new GpProduceProduct();
                gpProduceProduct.setProductName(gpPurchasingMakingDetails.getProductName());
                gpProduceProduct.setColour(gpPurchasingMakingDetails.getProductColor());
                iGpProduceProductService.save(gpProduceProduct);
            }
        }
        // 判断是否需要新增欧根纱成分基础表数据
        if (gpPurchasingMakingDetails.getFabricWeight() != null
                && gpPurchasingMakingDetails.getLowerMachineWidth() != null
                && StringUtils.isNotBlank(gpPurchasingMakingDetails.getDensity())
                && StringUtils.isNotBlank(gpPurchasingMakingDetails.getProductName())
                && StringUtils.isNotBlank(gpPurchasingMakingDetails.getProductColor())
                && StringUtils.isNotBlank(gpPurchasingMakingDetails.getSupplier())) {
            GpIngredientsTable gpIngredientsTable = iGpIngredientsTableService.getOne(new LambdaQueryWrapper<GpIngredientsTable>()
                    .eq(GpIngredientsTable::getFabricWeight, gpPurchasingMakingDetails.getFabricWeight())
                    .eq(GpIngredientsTable::getDoorWidth, gpPurchasingMakingDetails.getLowerMachineWidth())
                    .eq(GpIngredientsTable::getDensity, gpPurchasingMakingDetails.getDensity())
                    .eq(GpIngredientsTable::getProductName, gpPurchasingMakingDetails.getProductName())
                    .eq(GpIngredientsTable::getStockColor, gpPurchasingMakingDetails.getProductColor())
                    .eq(GpIngredientsTable::getSupplier, gpPurchasingMakingDetails.getSupplier())
                    .eq(GpIngredientsTable::getDelStatus, YesNoEnum.NO.getKey())
                    .last("limit 1"));
            if (gpIngredientsTable == null) {
                gpIngredientsTable = new GpIngredientsTable();
                gpIngredientsTable.setFabricWeight(gpPurchasingMakingDetails.getFabricWeight());
                gpIngredientsTable.setDoorWidth(gpPurchasingMakingDetails.getLowerMachineWidth());
                gpIngredientsTable.setDensity(gpPurchasingMakingDetails.getDensity());
                gpIngredientsTable.setProductName(gpPurchasingMakingDetails.getProductName());
                gpIngredientsTable.setStockColor(gpPurchasingMakingDetails.getProductColor());
                gpIngredientsTable.setSupplier(gpPurchasingMakingDetails.getSupplier());
                iGpIngredientsTableService.save(gpIngredientsTable);
            }
        }
    }

    private String getSn() {
        String sn = "";
        String yyMMdd = DateUtils.getDate("yyMMdd");
        sn += yyMMdd;
        GpPurchasingMakingDetails productionDetails = this.getOne(new LambdaQueryWrapper<GpPurchasingMakingDetails>()
                .eq(GpPurchasingMakingDetails::getDelStatus, YesNoEnum.NO.getKey())
                .isNotNull(GpPurchasingMakingDetails::getSn)
                .gt(GpPurchasingMakingDetails::getCreateTime, DateUtils.getDate("yyyy-MM-dd 00:00:00"))
                .orderByDesc(GpPurchasingMakingDetails::getCreateTime)
                .last("limit 1"));
        if (productionDetails == null) {
            sn += "-1";
        } else {
            sn += productionDetails.getSn().substring(productionDetails.getSn().lastIndexOf("-"));
        }
        return sn;
    }
}
